#include "processbar.hpp"
#include <cstring>
#include <unistd.h>


const char *lable = "|/-\\";

void process()
{
    char buffer[NUM];
    memset(buffer, 0, sizeof(buffer));
    int cnt = 0, n = sizeof(lable);
    buffer[0] = Head;
    while (cnt <= 100)
    {
        printf("[%-100s][%3d%%][%c]\r", buffer, cnt, lable[cnt % n]);
        fflush(stdout);
        buffer[cnt++] = Body;
        if (cnt < 100)
            buffer[cnt] = Head;
        usleep(50000);
    }

    std::cout << std::endl;
}

//version 2 
char buffer[NUM] = {0};
void process_flush(double rate)
{
    static int cnt = 0;
    int n = strlen(lable);
    if (rate <= 1.0)
        buffer[0] = Head;
    printf("[\033[4;32;44m%-100s\033[0m][%.1f%%][%c]\r", buffer, rate, lable[cnt % n]);
    fflush(stdout);

    buffer[(int)rate] = Body;
    if ((int)rate + 1 < 100)
        buffer[(int)(rate + 1)] = Head;
    if (rate >= 100.0)
        std::cout << std::endl;

    cnt++;
    cnt %= n; 
}
